package com.im;

import mapgenerator.*;
import com.utils.MapFileFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author Sajarwo Anggai
 */
public class IMControlPanel implements ActionListener {

    public static BufferedImage imgBackground;
    private Main comp;
    public String currentImageName;
    double preferedRows, preferedCols;

    /**
     * Managemen untuk Image Manager
     * @param comp
     */
    public IMControlPanel(Main comp) {
        this.comp = comp;
        this.showImgeManagerInfo(false);
        comp.IMButtonAddToPattern.addActionListener(this);
        comp.IMButtonAddToSprite.addActionListener(this);
        comp.IMButtonRemove.addActionListener(this);
        comp.IMButtonRename.addActionListener(this);

        try {
            imgBackground = ImageIO.read(getClass().getResource("/com/images/BackgroundWidget.png"));
        } catch (IOException ex) {
        }
    }

    /**
     * Menampilkan informasi pada Image Manager
     * @param activate - set true untuk menampilkan informasi
     */
    public void showImgeManagerInfo(boolean activate) {
        comp.IMButtonAddToPattern.setVisible(activate);
        comp.IMButtonAddToSprite.setVisible(activate);
        comp.IMButtonRemove.setVisible(activate);
        comp.IMButtonRename.setVisible(activate);
        comp.IMWidth.setVisible(activate);
        comp.IMHeight.setVisible(activate);
        comp.IMWidthValue.setVisible(activate);
        comp.IMHeightValue.setVisible(activate);
        comp.IMPrevWidth.setVisible(activate);
        comp.IMPrevWidthValue.setVisible(activate);
        comp.IMPrevHeight.setVisible(activate);
        comp.IMPrevHeightValue.setVisible(activate);
    }

    /**
     * Setting informasi image yang akan diolah
     * @param width - lebar dengan ukuran pixel
     * @param height - tinggi dengan ukuran pixel
     * @param size - ukuran image file
     */
    public void setImageManagerInfo(int width, int height, double cols, double rows) {
        comp.IMWidthValue.setText(": " + width + "px");
        comp.IMHeightValue.setText(": " + height + "px");
        comp.IMPrevWidthValue.setText(": " + cols);
        comp.IMPrevHeightValue.setText(": " + rows);        
        preferedCols = cols;
        preferedRows = rows;
    }

    public void actionPerformed(ActionEvent e) {
        if (!comp.project.isCreated()) {
            return;
        }
        Object ob = e.getSource();
        if (ob == comp.IMButtonRemove) {
            int val = JOptionPane.showConfirmDialog(comp, "Do you want to remove this image?", "Remove Image", JOptionPane.YES_NO_OPTION);
            if (val == JOptionPane.YES_OPTION) {
                comp.ipContainer.remove(currentImageName);
                comp.imContainer.removeImage(currentImageName);
                comp.imPanelWidget.reloadAll();
                comp.ipPanelWidget.reloadAll();
                comp.osPanelWidget.reloadAll();
                comp.imControlPanel.showImgeManagerInfo(false);
            }
        } else if (ob == comp.IMButtonRename) {
            renameImage(currentImageName);
        } else if (ob == comp.IMButtonAddToPattern) {
            IMToImagePatternDialog patternDialog = new IMToImagePatternDialog(comp, currentImageName, false);
            patternDialog.jSpinnerColumns.setValue(preferedCols);
            patternDialog.jSpinnerRows.setValue(preferedRows);
            patternDialog.setModal(true);
            patternDialog.setVisible(true);
        } else if (ob == comp.IMButtonAddToSprite) {
            IMToImagePatternDialog patternDialog = new IMToImagePatternDialog(comp, currentImageName, true);
            patternDialog.setModal(true);
            patternDialog.setVisible(true);
        }
    }

    /**
     * Set nama file yang aktif
     * @param location - test.png
     */
    void setImageContainerActive(String currentImageName) {
        this.currentImageName = currentImageName;
    }

    public void renameImage(String currentImageName) {
        String message = (String) JOptionPane.showInputDialog(comp, "Please rename it bellow", "Rename File", JOptionPane.OK_CANCEL_OPTION);
        try {
            if (message.equals("")) {
                return;
            }
        } catch (NullPointerException ev) {
            return;
        }
        File file = new File(comp.project.getImageDir() + File.separator + currentImageName);
        String fileDest = file.getParentFile() + File.separator + message.trim() + "." + MapFileFilter.getExtension(file);
        File ren = new File(fileDest);

        while (true) {
            if (ren.exists()) {
                String msg = "File can't rename because has been exist!\n" +
                        "Please rename it bellow";
                message = (String) JOptionPane.showInputDialog(comp, msg, "Rename File", JOptionPane.OK_CANCEL_OPTION);
                try {
                    if (message.equals("")) {
                        return;
                    }
                } catch (NullPointerException ev) {
                    return;
                }
                fileDest = file.getParentFile() + File.separator + message.trim() + "." + MapFileFilter.getExtension(file);
                ren = new File(fileDest);
                if (file.renameTo(ren)) {
                    break;
                }
            } else {
                file.renameTo(ren);
                break;
            }
        }
        comp.imContainer.renameImage(file.getName(), ren.getName());
        comp.ipContainer.setListImage(comp.ipContainer.getListImagePatternMapping().indexOf(currentImageName), ren.getName());
        comp.imPanelWidget.reloadAll();
        comp.ipPanelWidget.reloadAll();
        comp.imControlPanel.showImgeManagerInfo(false);
    }
}
